home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / boosters.arc / WORD.PAS < prev    next >
Pascal/Delphi Source File  |  1985-11-13  |  1KB  |  58 lines

  1. { ------------------------------------------
  2.   WORD returns a string that is word N of S.
  3.   ------------------------------------------ }
  4. Function Word  ( S : AnyString;
  5.                  N : Integer ) : AnyString;
  6.  
  7. var
  8.    NumWords, start, stop, CurrentAddress, len
  9.              : integer;
  10.    Ts
  11.              : AnyString;
  12.    BlankFound
  13.              : Boolean;
  14.  
  15.  
  16. begin
  17.    if Length(S) = 0 then
  18.       Word := ''
  19.    else
  20.    begin
  21.       NumWords := 0;
  22.       start := 1;
  23.       len := length(S);
  24.       stop := len;
  25.       BlankFound := True;
  26.       CurrentAddress := 0;
  27.  
  28.       repeat
  29.          CurrentAddress := CurrentAddress + 1;
  30.          if BlankFound then
  31.          begin
  32.             if S[CurrentAddress] <> #32 then
  33.             begin
  34.                BlankFound := false;
  35.                NumWords := NumWords + 1;
  36.                if NumWords = N then
  37.                   start := CurrentAddress;
  38.             end;
  39.          end
  40.          else
  41.          if S[CurrentAddress] = #32 then
  42.          begin
  43.             BlankFound := true;
  44.             if NumWords = N then
  45.                stop := CurrentAddress;
  46.          end;
  47.       until (stop < len) or (CurrentAddress = len);
  48.  
  49.       if N > NumWords then
  50.          Word := ''
  51.       else
  52.       begin
  53.          if S[stop] <> #32 then
  54.             stop := succ(stop);
  55.          Word := copy ( S, start, stop - start );
  56.       end;
  57.    end;
  58. end { Word };